/**
 * Licensed to iNet Academy under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Open-Ones Group licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package inet.android.buoi2;


// Activity1
// get two input values from user, put them in a bumble
// call Activity2 to add the two numbers, show result
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class iNETAndroidExplicit extends Activity {
    TextView txtVal1;
    EditText txtVal2;
    TextView lblResult;
    Button   btnAdd;    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        //error 1 ==> ?
        //txtVal1 = (TextView)findViewById(R.id.EditText01);
        txtVal2 = (EditText)findViewById(R.id.EditText02);
        //lblResult = (TextView) findViewById(R.id.TextView01);
        
        //error 2 ==> ? ?
        //btnAdd = (Button) findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// get values from the UI
				Double v1 = Double.parseDouble(txtVal1.getText().toString());
				Double v2 = Double.parseDouble(txtVal1.getText().toString());
				
				// create intent to call Activity2
				//error 3 - ?
				Intent myIntentA1A2 = new Intent (iNETAndroidExplicit.this,
												  Activity2.class);
				// create a container to ship data
				Bundle myData = new Bundle();
				
				//error 3 - ? hint: debug
				// add <key,value> data items to the container
				myData.putDouble("val1", v1);
				myData.putDouble("val2", v1);
				
				// attach the container to the intent
				myIntentA1A2.putExtras(myData);
				
				// call Activity2, tell your local listener to wait response
				startActivityForResult(myIntentA1A2, 101);
				
			}
				
		});
    }//onCreate

    //////////////////////////////////////////////////////////////////////////////
    // local listener receiving callbacks from other activities
	@Override
	protected void onActivityResult(int requestCode, int resultCode, 
			Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		try	{
			if ((requestCode == 101 ) && 
					(resultCode == Activity.RESULT_OK)){
				Bundle myResults = data.getExtras();
				Double vresult = myResults.getDouble("vresult");
				//error 5 - ?
				lblResult.setText("Sum is " + vresult);
			}
		}
		catch (Exception e) {
			lblResult.setText("Problems - " + requestCode + 
					" " + resultCode);
		}
	}//onActivityResult
	
    
}//Activity1